package com.zhuss.order.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.order.order.CareOrderBo;
import com.zhuss.bean.bo.order.order.app.ListTicketBo;
import com.zhuss.bean.vo.order.order.CareOrderVo;
import com.zhuss.bean.vo.order.order.DetailOrderVo;
import com.zhuss.bean.vo.order.order.app.ListTicketVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.OrderStatus;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.order.db.dao.ext.OrderDao;
import com.zhuss.order.db.dao.ext.OrderItemDao;
import com.zhuss.order.db.dao.ext.PrepayDao;
import com.zhuss.order.processor.unuse.OrderUnuse;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.util.DateUtil;
import com.zhuss.util.JsonUtil;

/**
 * app
 * 查询车票列表
 * 回写支付单状态 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("listTicketForUpdate")
public class ListTicketForUpdate extends AbstractProcessor<PageVo<ListTicketVo>, ListTicketBo> implements Processor<PageVo<ListTicketVo>, ListTicketBo> {

    @Autowired
    private OrderDao orderDao;
    
    @Autowired
    private OrderRead orderRead;
    
    @Autowired
    private OrderItemDao orderItemDao;
    
    @Autowired
    private PrepayDao prepayDao;
    
    @Autowired
    private TrainDao trainDao;
    
    @Autowired
    private OrderCheck orderCheck;
    
    @Autowired
	@Qualifier("careOrderForUpdate")
	private Processor<CareOrderVo, CareOrderBo> careOrderForUpdate;
    
    
    /**
     * 校验
     */
    @Override
    public void check(ListTicketBo param) throws DzcxException {
    	
    }
    
    /**
     * 操作方法
     * @throws DzcxException 
     */
    @Override
    public PageVo<ListTicketVo> process(ListTicketBo param) throws DzcxException {
    	Date startDate = DateUtil.toDateOnly(new Date());
    	param.setStartDate(startDate);
    	
    	PageVo<ListTicketVo> result = new PageVo<ListTicketVo>();
        result.setPageNum(param.getPageNum());
        result.setPageSize(param.getPageSize());
        refresh(orderItemDao.listTicket(param), param);
        param.setIsTicket(IsEnum.是.key());
        result.setItems(orderItemDao.listTicket(param));
        result.setTotal(orderItemDao.countListTicket(param));
		return result;
    }
    
    private List<ListTicketVo> refresh(List<ListTicketVo> result, ListTicketBo param) throws DzcxException {
    	for (ListTicketVo item : result) {
    		Long orderId = item.getOrderId();
        	DetailOrderVo order = orderRead.detail(orderId);
        	
        	Integer status = order.getStatus();
        	if (OrderStatus.未支付.sameKey(status) || OrderStatus.已提交支付.sameKey(status)) {
        		try{
        			OrderUnuse.startUnuseDecrUsed(order, orderCheck, orderDao, orderItemDao, trainDao, param.getConcurrentUserName(), param.getConcurrentUserId(), status);
        		} catch (DzcxException e) {
        			if (!ExceptionCode.订单已过期.getCode().equals(e.getCode())) {
        				throw e;
        			}
        			
        		}
        	}
        	
        	if (OrderStatus.已提交支付.sameKey(status) || OrderStatus.用户支付中.sameKey(status)) {
        		CareOrderBo bo = new CareOrderBo();
        		BeanUtils.copyProperties(param, bo);
        		bo.setOrderId(item.getOrderId());
        		careOrderForUpdate.process(bo);
        	}
    	}
    	return result;
	}

	/**
     * 操作日志
     */
	@Override
	public PageVo<ListTicketVo> log(ListTicketBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.订单.key());
    	record.setAction(LogActionEnum.同步车票列表状态查询.key());
    	record.setTargetId(null);
    	record.setTargetName(param.getConcurrentUserId() + "");
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
